package com.granada.behaviour.pc;


import com.granada.entity.SysUser;
import com.granada.behaviour.base.UserAuthBehavior;
import com.granada.exception.BusinessException;
import com.granada.exception.code.BaseResponseCode;
import com.granada.utils.DateUtils;
import com.granada.utils.MapperBeanUtils;
import com.granada.utils.PasswordUtils;
import com.granada.vo.req.LoginReqVO;
import com.granada.vo.resp.UserLoginAppRespVO;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class UserAuthWithPcBehavior extends UserAuthBehavior {
    @Override
    public String refreshToken(String refreshToken, String accessToken) throws Exception {
        return null;
    }

    @Override
    public List<UserLoginAppRespVO> getUserLoginInfo(Long userId) throws Exception {
        return null;
    }

    @Override
    public Boolean logging(LoginReqVO vo) throws Exception {
        SysUser sysUser = MapperBeanUtils.getSysUserMapper().getUserInfoByName(vo.getUsername());
        if (null == sysUser) {
            throw new BusinessException(BaseResponseCode.NOT_ACCOUNT);
        }

        if (sysUser.getStatus() == 2) {
            throw new BusinessException(BaseResponseCode.USER_LOCK);
        }
        if (!PasswordUtils.matches(sysUser.getSalt(), vo.getPassword(), sysUser.getPassword())) {
            throw new BusinessException(BaseResponseCode.PASSWORD_ERROR);
        }
//        if(StringUtils.isBlank(sysUser.getRealName()) || StringUtils.isBlank(sysUser.getCompanyName()) || StringUtils.isBlank(sysUser.getDeptId())
//                ||StringUtils.isBlank(sysUser.getPhone())||StringUtils.isBlank(sysUser.getPosition())){
//            respVO.setMessage(Constant.REMINDER_MSG);
//        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();
        String loginTimeStr = sdf.format(calendar.getTime());//当前时间
        Date loginTime = sdf.parse(loginTimeStr);
        //判断要比较当前登录时间要在数据库中开始自动生成的时间和结束时间之间
        Date startTime = sysUser.getStartTime();//注册时间
        Date lastTime = sysUser.getLastTime();//过期时间
        Boolean flag = DateUtils.belongCalendar(loginTime, startTime, lastTime);
        return flag;
    }
}
